package com.ease.integration.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ease.common.util.FileUtil;
import com.ease.integration.definition.constant.IntegrationMessageTemplateReserveKey;
import com.ease.oxm.jaxb.JaxbUtil;

/**
 * 外部参数和java对象相互转换
 * 
 * @author nathanleewei
 * 
 */
public abstract class ArgumentObjectInterconverter {

	private static final transient Log log = LogFactory.getLog(ArgumentObjectInterconverter.class);

	/**
	 * 先把map形式的调用参数转为bean的xml表示,再用jaxb把xml映射为java对象
	 * 
	 * @param <T>
	 * @param parameters
	 * @param template
	 * @param clazz
	 * @return
	 */
	public static <T> T parameterToObject(Map<String, Object> parameters, String template, Class<T> clazz) {
		String formattedXml = ArgumentXmlFormatConverter.format(parameters, template);
		if (log.isDebugEnabled()) {
			log.debug("格式化后xml: \r\n" + formattedXml);
		}
		T object = JaxbUtil.xmlToObject(formattedXml, clazz);
		return (T) object;
	}

	/**
	 * 把java对象转为调用参数形式的(string,int,date,xml)
	 * 
	 * @param objects
	 * @param template
	 * @return
	 */
	public static Map<String, Object> objectToParameters(Map<String, Object> objects, String template) {
		Map<String, Object> result = new HashMap<String, Object>();
		String str = ""; // FreeMarkerSupport.getInstance().processPath(template, objects);
		String[] arguments = str.split(IntegrationMessageTemplateReserveKey.ARGUMENTS_SPLITER);
		for (int i = 0; i < arguments.length; i++) {
			String argument = arguments[i];
			String key = argument.substring(4, argument.indexOf(";\r\ntype="));
			String type = argument.substring(argument.indexOf(";\r\ntype=") + 8, argument.indexOf(";\r\nvalue="));
			String value = argument.substring(argument.indexOf(";\r\nvalue=") + 9);

			if ("string".equalsIgnoreCase(type)) {
				result.put(key, value);
			} else if ("int".equalsIgnoreCase(type)) {
				int intValue = 0;
				try {
					intValue = Integer.parseInt(value);
				} catch (Exception e) {
					log.error(e, e);
				}
				result.put(key, intValue);
			} else if ("boolean".equalsIgnoreCase(type)) {
				boolean bValue = false;
				try {
					bValue = Boolean.parseBoolean(value);
				} catch (Exception e) {
					log.error(e, e);
				}
				result.put(key, bValue);
			} else if ("xml".equalsIgnoreCase(type)) {
				result.put(key, value);
				result.put(IntegrationMessageTemplateReserveKey.XML_STRING + key, value);
			}
		}
		return result;
	}

	/**
	 * 找到请求模板中xml格式的参数的名字
	 * 
	 * @param template
	 * @return
	 */
	public static List<String> getKeysOfXmlStyleRequestArgument(String template) {
		String str = FileUtil.readClassPathResourceToString(template);

		if (null != str) {
			List<String> keys = new ArrayList<String>();
			String[] arguments = str.split(IntegrationMessageTemplateReserveKey.ARGUMENTS_SPLITER);
			for (int i = 0; i < arguments.length; i++) {
				String argument = arguments[i];
				int start = argument.indexOf("key=");
				String key = argument.substring(start + 4, argument.indexOf(";\r\ntype="));
				String type = argument.substring(argument.indexOf(";\r\ntype=") + 8, argument.indexOf(";\r\nvalue="));

				if ("xml".equalsIgnoreCase(type)) {
					keys.add(key);
				}
			}
			return keys;
		}
		return new ArrayList<String>();
	}

}
